package com.chaofan.authsso.core.service.impl;

import cn.hutool.core.util.StrUtil;
import com.chaofan.authsso.core.common.LocalCache;
import com.chaofan.authsso.core.service.CacheInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Author: Chaofan
 * Date: 2020/6/5 14:12
 * email: chaofan2685@qq.com
 */
public class LocalCacheInstanceImpl implements CacheInstance {

    public static final Logger logger = LoggerFactory.getLogger(LocalCacheInstanceImpl.class);

    private ConcurrentHashMap<String, LocalCache> props = new ConcurrentHashMap<>();

    public LocalCacheInstanceImpl() {
        //定义一个线程,定时删除props里面过期的数据
        final long timeInterval = 3600000;
        new Thread() {
            public void run() {
                List<String> propKeys = new ArrayList<>();
                while (true) {
                    propKeys.clear();
                    for (Map.Entry<String, LocalCache> entry : props.entrySet()){
                        if (entry.getValue().isExpires()){
                            propKeys.add(entry.getKey());
                        }
                    }
                    for (String prop : propKeys){
                        props.remove(prop);
                    }
                    logger.info("Timed cache clearance");
                    try {
                        Thread.sleep(timeInterval);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    public Object get(String key) {
        if (props.containsKey(key)){
            LocalCache localCache = props.get(key);
            if (localCache.isNotExpires()){
                return localCache.getValue();
            }else {
                props.remove(key);
            }
        }
        return null;
    }

    @Override
    public boolean containsKey(String key) {
        return props.containsKey(key);
    }

    @Override
    public boolean set(String key, Object value) {
        if (value!=null && StrUtil.isNotEmpty(key)){
            props.put(key,new LocalCache(value));
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean set(String key, Object value, long time) {
        if (value!=null && StrUtil.isNotEmpty(key) && time>0){
            props.put(key,new LocalCache(time,value));
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void del(String... key) {
        if(key!=null&&key.length>0){
            if(key.length==1){
                props.remove(key[0]);
            }else{
                for (String k : key){
                    props.remove(k);
                }
            }
        }
    }

    @Override
    public boolean expire(String key, long time) {
        Object value = get(key);
        if (value != null){
            return set(key,value,time);
        }else {
            return false;
        }
    }

}
